package org.michaelbel.bottomsheet.adapter;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.utils.Color;
import ohos.app.Context;
import org.michaelbel.bottomsheet.util.ResTUtil;

import java.util.HashMap;
import java.util.List;

import static ohos.agp.utils.LayoutAlignment.TOP;
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * grid 适配器
 * @param <T> T
 */
public abstract class EasyGridProvider<T> extends RecycleItemProvider {
    private Context context;
    private List<T> data;
    private int mLayoutId;
    private int numColumns = 1;
    private OnItemClickListener onItemClickListener;
    private boolean isTheme;
    /**
     * @param context   上下文
     * @param data      数据源
     * @param mLayoutId 条目的资源文件id
     * @return EasyGridProvider
     */
    public EasyGridProvider(Context context, int mLayoutId, List<T> data, boolean isTheme) {
        this.context = context;
        this.data = data;
        this.mLayoutId = mLayoutId;
        this.isTheme = isTheme;
    }

    /**
     * 设置列数
     * @param numColumns numColumns
     */
    public void setNumColumns(int numColumns) {
        this.numColumns = numColumns;
    }

    /**
     * 设置数据
     * @param data data
     */
    public void setData(List<T> data) {
        this.data = data;
        notifyDataChanged();
    }

    /**
     * 获取数据
     * @return List
     */
    public List<T> getData() {
        return data;
    }

    @Override
    public int getCount() {
        if (data != null) {
            return data.size() % numColumns == 0 ? data.size() / numColumns : data.size() / numColumns + 1;
        } else {
            return 0;
        }
    }

    @Override
    public T getItem(int position) {
        if (data != null) {
            return data.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertComponent, ComponentContainer parent) {
        ViewHolder viewHolder;
        convertComponent = new DirectionalLayout(context);
        ((DirectionalLayout) convertComponent).setOrientation(Component.HORIZONTAL);
        ComponentContainer.LayoutConfig layoutConfig = convertComponent.getLayoutConfig();
        layoutConfig.width = DependentLayout.LayoutConfig.MATCH_PARENT;
        layoutConfig.height = DependentLayout.LayoutConfig.MATCH_CONTENT;
        convertComponent.setLayoutConfig(layoutConfig);
        for (int i = 0; i < numColumns; i++) {
            if (position * numColumns + i < data.size()) {
                DirectionalLayout dlItemParent = new DirectionalLayout(context);
                dlItemParent.setLayoutConfig(
                        new DirectionalLayout.LayoutConfig(0, DirectionalLayout.LayoutConfig.MATCH_CONTENT, TOP, 1));
                Component childConvertComponent = LayoutScatter.getInstance(context).parse(mLayoutId, null, false);
                int finalI = i;
                childConvertComponent.setClickedListener(
                        new Component.ClickedListener() {
                            @Override
                            public void onClick(Component component) {
                                if (onItemClickListener != null) {
                                    onItemClickListener.onItemClick(component, position * numColumns + finalI);
                                }
                            }
                        });
                dlItemParent.addComponent(childConvertComponent);
                ((ComponentContainer) convertComponent).addComponent(dlItemParent);
                viewHolder = new ViewHolder(childConvertComponent);
                bind(viewHolder, getItem(position * numColumns + i), position * numColumns + i);
            } else {
                // 用Component占位会导致高度为MATCH_PARENT,所以此处用DirectionalLayout占位
                DirectionalLayout childConvertComponent = new DirectionalLayout(context);
                childConvertComponent.setLayoutConfig(
                        new DirectionalLayout.LayoutConfig(0, DirectionalLayout.LayoutConfig.MATCH_CONTENT, TOP, 1));
                childConvertComponent.setBackground(ResTUtil.buildDrawableByColor(0xffffff));
                ((ComponentContainer) convertComponent).addComponent(childConvertComponent);
            }
        }
        return convertComponent;
    }

    /**
     * 设置点击监听
     * @param onItemClickListener onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    /**
     * bind view
     * @param holder holder
     * @param st s
     * @param position position
     */
    protected abstract void bind(ViewHolder holder, T st, int position);

    /**
     * viewholder
     */
    protected static class ViewHolder {
        HashMap<Integer, Component> mViews = new HashMap<>();
        /**
         * itemView
         */
        public Component itemView;

        ViewHolder(Component component) {
            this.itemView = component;
        }

        /**
         * setText
         * @param viewId viewId
         * @param text text
         * @return ViewHolder
         */
        public ViewHolder setText(int viewId, String text) {
            ((Text) getView(viewId)).setText(text);
            return this;
        }

        /**
         * setTextColor
         * @param viewId viewId
         * @param color color
         * @return ViewHolder
         */
        public ViewHolder setTextColor(int viewId, Color color) {
            ((Text) getView(viewId)).setTextColor(color);
            return this;
        }

        /**
         * setBackGroundColor
         * @param viewId viewId
         * @param element element
         * @return ViewHolder
         */
        public ViewHolder setBackGroundColor(int viewId, Element element) {
            ((ComponentContainer) getView(viewId)).setBackground(element);
            return this;
        }

        /**
         * setImg
         * @param viewId viewId
         * @param element element
         * @return ViewHolder
         */
        public ViewHolder setImg(int viewId, Element element) {
            ((Image) getView(viewId)).setImageElement(element);
            return this;
        }

        /**
         * getView
         * @param viewId viewId
         * @param <E> E
         * @return E
         */
        public <E extends Component> E getView(int viewId) {
            E view = (E) mViews.get(viewId);
            if (view == null) {
                view = (E) itemView.findComponentById(viewId);
                mViews.put(viewId, view);
            }
            return view;
        }
    }

    /**
     * 点击监听
     */
    public abstract static class OnItemClickListener {
        /**
         * 监听
         * @param component component
         * @param position position
         */
        public abstract void onItemClick(Component component, int position);
    }
}
